﻿using System;

namespace BA.MultiMvc.Framework.NHibernate
{
    /// <summary>
    /// Defines an abstract NHibernate entity.  
    /// Defines the identity of a class and the Equals and GetHashCode
    /// </summary>
    /// <typeparam name="TId"></typeparam>
    public abstract class Entity<TId>
    {
        public virtual TId Id { get; protected set; }

        protected virtual int Version { get; set; }

        public override bool Equals(object obj)
        {
            return Equals(obj as Entity<TId>);
        }

        private static bool IsTransient(Entity<TId> obj)
        {
            return obj != null &&
            Equals(obj.Id, default(TId));
        }

        private Type GetUnproxiedType()
        {
            return GetType();
        }

        public virtual bool Equals(Entity<TId> other)
        {
            if (other == null) 
                return false;

            if (ReferenceEquals(this, other)) 
                return true;

            if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) || otherType.IsAssignableFrom(thisType);
            }

            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(TId)))
                return base.GetHashCode();
            
            return Id.GetHashCode();
        }
    }

    /// <summary>
    /// Entity who has a Guid as primary key
    /// </summary>
    public abstract class Entity:Entity<Guid>
    {
        
    }
}